package com.zhang.third.day02;

import com.zhang.third.utils.ClickEventSource;
import com.zhang.third.utils.Event;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @title: filter
 * @author: zhang
 * @date: 2022/4/2 12:32
 */
public class Example3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickEventSource())
                .filter(new FilterFunction<Event>() {
                    @Override
                    public boolean filter(Event value) throws Exception {
                        return value.key.equals("Mary");
                    }
                })
                .print("匿名类实现filter");

        env
                .addSource(new ClickEventSource())
                .filter(new MyFilter())
                .print("外部类实现filter");

        env
                .addSource(new ClickEventSource())
                .filter(r -> r.key.equals("Mary"))
                .print("匿名函数实现filter");

        env
                .addSource(new ClickEventSource())
                .flatMap(new FlatMapFunction<Event, Event>() {
                    @Override
                    public void flatMap(Event value, Collector<Event> out) throws Exception {
                        if (value.key.equals("Mary")) out.collect(value);
                    }
                })
                .print("flatMap实现filter");

        env.execute();
    }

    public static class MyFilter implements FilterFunction<Event> {
        @Override
        public boolean filter(Event value) throws Exception {
            return value.key.equals("Mary");
        }
    }
}
